/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: UserCrawl.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.user.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月23日 上午11:59:38 
 * @version: V1.0   
 */
package com.zizo.autohome.user.crawl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.user.bean.User;
import com.zizo.autohome.user.dao.UserDAO;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.core.Errors;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: UserCrawl
 * @Description:
 * @author: Alexis
 * @date: 2016年7月23日 上午11:59:38
 */
public class UserCrawl extends Crawl {
	private UserDAO userdao = (UserDAO) this.dao;

	/**
	 * @Title:UserCrawl
	 * @Description:
	 * @param id
	 * @param dao
	 * @param error
	 */
	public UserCrawl(String id, DAO dao, List<Errors> error) {
		super(id, dao, error);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		Elements pagesElement = doc.select(selectStr);
		int pages = Integer.parseInt(pagesElement.get(pagesElement.size() - 2).text());
		return pages;
	}

	/*
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {

	}

	/*
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {
		try {
			Document doc = DocUtil.getDoc(url);// 获得http://i.autohome.com.cn/6393663的文档用于分析
			if (doc == null || doc.text().contains("您访问的页面出错了")) {
				return;
			}
			String userName = doc.select("h1.user-name b").first().text();// 用户姓名
			int attentionCount = 0;
			if (!doc.select("a.state-mes").first().text().contains("-1")) {
				attentionCount = Integer
						.parseInt(Util.getMatched(doc.select("a.state-mes").first().text(), "(.* )(\\d+)").group(2));// 关注数量
			}
			int fansCount = 0;
			if (!doc.select("a.state-mes").get(1).text().contains("-1")) {
				fansCount = Integer
						.parseInt(Util.getMatched(doc.select("a.state-mes").get(1).text(), "(.* )(\\d+)").group(2));// 粉丝数量
			}

			String location = doc.select("a.state-pos").text();// 所在地
			String garageUrl = "http://i.autohome.com.cn/" + id + "/car?page=1";
			Document docGarage = DocUtil.getDoc(garageUrl);// 车库的Document，重新获得一个
			int garageCount = Integer.parseInt(docGarage.select("span.fcolor_6").first().text());// 车库数量
			List<String> garageIDs = new ArrayList<>();// 车库ID的集合,插入数据库的时候，以','分开
			Elements focusCarLis = docGarage.select("ul.focusCar li");
			if (garageCount > 10) {
				int pages = getPages(docGarage, "div.page a");// 每页只能显示10个车，查看共有多少页
				for (int i = 2; i < pages + 1; i++) {
					focusCarLis.addAll(DocUtil.getDoc("http://i.autohome.com.cn/" + id + "/car?page=" + i)
							.select("ul.focusCar li"));
				}
			}
			List<String> certifications = new ArrayList<>();// 认证的车子
			int mostFocus = 0;// 最关注的车子
			int running = 0;// 正在开
			/* 把该用户的所有车库添加到List<String>集合中 */
			for (Element focusCar : focusCarLis) {
				String carid = focusCar.select("img").first().attr("id").split("_")[1];
				garageIDs.add(carid);
				if (focusCar.select("a.rzcz").attr("style").length() == 0) {
					certifications.add(carid);
				}
				if (focusCar.select("p.m_t8 span.fcolor_6").text().contains("最关注的车型")) {
					mostFocus = Integer.parseInt(carid);
				}
				if (focusCar.select("p.m_t8 span.fcolor_6").text().contains("在开的车")) {
					running = Integer.parseInt(carid);
				}
			}
			User user = new User(Integer.parseInt(this.id), userName, attentionCount, fansCount, location, garageCount,
					garageIDs, certifications, mostFocus, running);
			userdao.insertData(user);
			System.out.println(user);
		} catch (Exception e) {
			System.out.println(url);
			e.printStackTrace();
		}
	}

	/*
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		crawlContent("http://i.autohome.com.cn/" + id);
		System.out.println(id + "完成");
		testUser();
		System.out.println(id + "完成");
	}

	/**
	 * @Title: testUser
	 * @Description: 验证用户是否存在，如果存在把用户的ID插入数据库中，不存在直接跳过。通过判断Document的内容判断用户是否存在
	 * @return: void
	 * @author: Alexis
	 */
	public void testUser() {
		String url = "http://i.autohome.com.cn/" + this.id;
		Document doc = DocUtil.getDoc(url);
		if (doc == null) {
			System.out.println(id + "error");
			return;
		}
		if (doc.text().contains("用户不存在")) {
			return;
		} else {
			userdao.insertUserID(this.id);
			System.out.println(this.id);
		}
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection();
		ExecutorService fix = Executors.newFixedThreadPool(25);
		UserDAO userdao = new UserDAO(currConn);
		List<String> userIDS = userdao.getIDs();
		for (String string : userIDS) {
			fix.execute(new UserCrawl(string, userdao, null));
		}
		fix.shutdown();
		List<Errors> error = new ArrayList<>();
		Connection currConn1 = ConnectionPool.getConnection("autohome_other", "localhost", "3306");
		UserDAO userdao1 = new UserDAO(currConn1);
		List<String> userIDS1 = userdao1.getIDs();
		for (String string : userIDS1) {
			UserCrawl user = new UserCrawl(string, userdao1, error);
			user.crawlContent(string);
			// user.crawlContent("http://i.autohome.com.cn/31239772");
		}

		System.out.println("wancheng");
	}

}
